using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NUnit.Framework;

namespace DatabaseUnitTesting.Utilities.StoredProcedure.Tests
{
    [TestFixture]
    public class SqlFileAdapterTests
    {
        [Test]
        public void SqlReaderWriterTest()
        {
            string filename = "C:/Temp/ReaderWriterTests.sql";
            string database = "astyler";
            string server = "(local)";
            string connectionString = "Application Name=UnitTests;Trusted_Connection=true;Server=" + server +
                                      ";Database=" + database;
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

            SqlCommand storedProcedure = new SqlCommand("TwoResultSets", connection);
            storedProcedure.CommandType = CommandType.StoredProcedure;
            List<string> commands =
                SqlGenerator.GenerateScript(ResultSetParser.Parse(storedProcedure), "dbo", "someTable");
            SqlFileAdapter.WriteCommandsToFile(commands, filename);
            List<string> otherCommands = SqlFileAdapter.ReadCommandsFromFile(filename);

            Assert.AreEqual(commands.Count, otherCommands.Count);

            foreach(string otherCommand in otherCommands)
                Assert.IsTrue(commands.Find(delegate(string item) { return item.Equals(otherCommand); }) !=
                              null);

            connection.Close();
        }
    }
}